package com.yabushan.web.utils;

/**
 * @Author yabushan
 * @Date 2020/11/5 10:04
 * @Version 1.0
 */

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * MD5加密工具类
 * @author pibigstar
 *
 */
public  class MyMD5Util {
    //盐，用于混交md5
    //private static final String slat = "&%5123***&&%%$$#@";
    public static String encrypt(String dataStr) {
        try {
           // dataStr = dataStr + slat;
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(dataStr.getBytes("UTF8"));
            byte s[] = m.digest();
            String result = "";
            for (int i = 0; i < s.length; i++) {
                result += Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public static void main(String[] args) {
        System.out.println(encrypt("sxf"));
        doGetTestWayOne();
    }




    public static  void  doGetTestWayOne() {
        String outUrl="https://times-dpctest.timesgroup.cn:5001/method?code=getVendorNameById&syscode=timesadmin&vendor_id=&vendor_name=电缆";

        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        /**
         * 行政管理系统配置
         * 1.	syscode: timesadmin
         * 2.	tokenStr:
         * 测试:  123test
         * 生产：prod473s2fd4g2dd30kpfRFH00S
         * 参数token产生规则：
         * token由当前时间(到分钟)+秘钥产生的MD5，MD5(YYYYMMDDHHMM + tokenStr)
         *
         * 接口配置
         * 1.	code: getVendorNameById
         * 2.	入参::
         * vendor_id:  供应商id
         * vendor_name：供应商名称
         */
        SimpleDateFormat myFmt=new SimpleDateFormat("yyyyMMddHHmm");
        String date = myFmt.format(new Date());
        String tokenStr=date+"123test";
        tokenStr= MyMD5Util.encrypt(tokenStr);
        // 参数
        StringBuffer params = new StringBuffer();

        params.append("&token="+tokenStr);
        outUrl+=params.toString();

        // 创建Get请求
        HttpGet httpGet = new HttpGet(outUrl);
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 配置信息
            RequestConfig requestConfig = RequestConfig.custom()
                    // 设置连接超时时间(单位毫秒)
                    .setConnectTimeout(5000)
                    // 设置请求超时时间(单位毫秒)
                    .setConnectionRequestTimeout(5000)
                    // socket读写超时时间(单位毫秒)
                    .setSocketTimeout(5000)
                    // 设置是否允许重定向(默认为true)
                    .setRedirectsEnabled(true).build();

            // 将上面的配置信息 运用到这个Get请求里
            httpGet.setConfig(requestConfig);

            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);

            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}

